#pragma once
#include<iostream>
#include<string>
#include<pthread.h>
class Thread
{
public:
    typedef enum
    {
        NEW = 1,
        RUNING,
        EXIT
    }status;
    typedef void* (*fun_t)(void*);
    Thread()
    {
        ;
    }
    Thread(int num, fun_t fun, void* arg)
    :id_(0),fun_(fun),arg_(arg),status_(NEW)
    {
        name_ = "thread->" + std::to_string(num);
    }
    ~Thread()
    {
        ;
    }
    static void * threadRun(void*arg)
    {
        Thread* th = (Thread*) arg;
        th->fun_(th->arg_);
        return nullptr;
    }
    void Run()
    {
        int n = pthread_create(&id_,nullptr,threadRun,(void*)this);
        if(n != 0)//成功返回0，不成功返回错误码
            exit(4);
        status_ = RUNING;    
    }
    void join()
    {
        pthread_join(id_,nullptr);
        status_ = EXIT;
    }
    std::string getname()
    {
        return name_;
    }
    int getstatus()
    {
        return status_;
    }
    pthread_t  getid()
    {
        if(status_ == RUNING)
            return id_;
        else
        {
            std::cout<<name_<<" not create ";
            return 1;
        }    
    }
    pthread_t id_;
    std::string name_;//线程名
    status status_;//线程状态
    fun_t fun_;//线程执行函数
    void* arg_;//线程参数
};